Page({
  data: {
    title: '',
    videos: [],
    currentVideo: null
  },

  onLoad: function(options) {
    console.log('接收到的参数:', options); // 添加日志
    const videos = JSON.parse(options.videos);
    console.log('解析后的视频数据:', videos); // 添加日志
    
    this.setData({
      title: options.title,
      videos: videos,
      currentVideo: videos[0]
    });
    
    this.videoContext = wx.createVideoContext('myVideo');
  },

  getVideoData: function(videoId, title) {
    // 这里应该从服务器获取数据，暂时使用模拟数据
    const allVideos = {
      '脸部护理': [
        { id: 1, url: '/videos/face1.mp4', poster: '/images/video-poster1.jpg' },
        { id: 2, url: '/videos/face2.mp4', poster: '/images/video-poster2.jpg' },
        { id: 3, url: '/videos/face3.mp4', poster: '/images/video-poster3.jpg' }
      ],
      '妆容教程': [
        { id: 4, url: '/videos/makeup1.mp4', poster: '/images/video-poster4.jpg' },
        { id: 5, url: '/videos/makeup2.mp4', poster: '/images/video-poster5.jpg' },
        { id: 6, url: '/videos/makeup3.mp4', poster: '/images/video-poster6.jpg' }
      ]
    };
    
    const currentVideo = allVideos[title].find(v => v.id == videoId);
    const relatedVideos = allVideos[title].filter(v => v.id != videoId);
    
    return {
      url: currentVideo.url,
      poster: currentVideo.poster,
      relatedVideos: relatedVideos
    };
  },

  playRelatedVideo: function(e) {
    const videoId = e.currentTarget.dataset.id;
    const videoData = this.getVideoData(videoId, this.data.title);
    
    this.setData({
      videoUrl: videoData.url,
      videoPoster: videoData.poster
    });
    
    this.videoContext.play();
  },

  onFullScreenChange: function(e) {
    if (!e.detail.fullScreen) {
      this.videoContext.pause();
    }
  },

  goBack: function() {
    wx.navigateBack();
  }
});